package control.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import model.Ap;
import model.Ip;

public class ArquivoUtil {

	public List<Ap> carregarIps() throws Exception {
		FileReader arq = new FileReader("ips.csv");
		BufferedReader buffer = new BufferedReader(arq);
		ArrayList<Ap> aps = null;
		aps = new ArrayList<Ap>();
		Ap ap = null;
		String prop =null; 
		do {
			prop = buffer.readLine();
			if (prop == null) {
				break;
			}
			
			String[] apsTexto = prop.split(";");
			String[] camposAps = null;
			
			for (String s : apsTexto) {
				camposAps = s.split(",");
				ap = new Ap(new Ip(camposAps[0]), camposAps[1], camposAps[2],
						camposAps[3]);
				aps.add(ap);
			}
		} while (true);

		arq.close();

		return aps;
	}

	public void salvarIps(List<Ap> ips) throws Exception {

		PrintWriter prop = new PrintWriter("ips.csv");

		for (Ap ap : ips) {
			prop.print(ap.getIp().getIp() + "," + ap.getNome() + ","
					+ ap.getDescricao() + "," + ap.getMarca() + ";\n");
		}
		prop.close();
	}
	
	public void salvarEmails(List<String> emails) throws Exception {
		PrintWriter prop = new PrintWriter("emails.csv");

		for (String s : emails) {
			prop.print(s + ";");
		}
		prop.close();
	}

	public List<String> carregarEmails() throws Exception {
		FileReader arq = new FileReader("emails.csv");
		BufferedReader buffer = new BufferedReader(arq);
		ArrayList<String> emails = null;
		do {
			String prop = buffer.readLine();

			if (prop == null) {
				break;
			}
			String[] campos = prop.split(";");
			emails = new ArrayList<String>();
			for (String s : campos) {
				emails.add(s);
			}
		} while (true);

		arq.close();
		return emails;
	}

}
